/**
 * 
 */
package com.siteseat.common.json;

import java.util.Date;

import org.codehaus.jackson.Version;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;

import com.siteseat.common.entity.ValueType;
import com.siteseat.common.security.Permission;

/**
 *
 */
public class JacksonMapper {

  public static final ObjectMapper  MAPPER = new ObjectMapper(); // can reuse,
                                                                 // share
                                                                 // globally

  private static final SimpleModule module;

  static {
    module = new SimpleModule( "m", new Version( 1, 0, 0, null ) );

    register( Date.class, new DateSerializer(), new DateDeserializer() );
    register( ValueType.class, new ValueTypeSerializer(), new ValueTypeDeserializer() );
    register( Permission.class, new PermissionSerializer(), new PermissionDeserializer() );
    
    MAPPER.registerModule( module );
    MAPPER.configure( Feature.ALLOW_COMMENTS, true );
  }

  public static <T> void register( Class<T> type, JsonSerializer<T> serializer, JsonDeserializer<T> deserializer ) {
    registerSerializer( serializer );
    registerDeserializer( type, deserializer );
  }

  public static <T> void registerSerializer( JsonSerializer<T> serializer ) {
    module.addSerializer( serializer );
  }

  public static <T> void registerDeserializer( Class<T> type, JsonDeserializer<T> deserializer ) {
    module.addDeserializer( type, deserializer );
  }

}
